package com.app.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.model.EntrepreneursModel;
import com.app.service.AdminUserService;
import com.app.service.EntrepreneursService;
import com.app.service.impl.AdminUserServiceImpl;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.app.vo.EntrepreneursVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 16693103420
 * @since 2024-02-01
 */
@RestController
@RequestMapping("/entrepreneurs")
public class EntrepreneursController {
    @Autowired
    private EntrepreneursService entrepreneursService;
    @Autowired
    private AdminUserService adminUserService;
    // 查询列表
    @GetMapping("/getEntrepreneursList")
    public Result getEntrepreneursList(@RequestParam(defaultValue = "0") Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit, HttpServletRequest request,@RequestParam(required = false) Integer areaId) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (user.getAreaId()==null){
            user.setAreaId(areaId);
        }
        EntrepreneursVo EntrepreneursVo = entrepreneursService.getEntrepreneursList(page, limit,user);
        return Result.success(EntrepreneursVo);
    }

    // 查询列表
    @GetMapping("/getUserEntrepreneursList")
    public Result getUserEntrepreneursList(@RequestParam(defaultValue = "0") Integer page,
                                           @RequestParam(defaultValue = "10") Integer limit) {
        EntrepreneursVo EntrepreneursVo = entrepreneursService.getUserEntrepreneursList(page, limit);
        return Result.success(EntrepreneursVo);
    }

    //根据id查询
    @GetMapping("/getEntrepreneursById")
    public Result getEntrepreneursById(Integer id) {
        EntrepreneursModel Entrepreneurs = entrepreneursService.getById(id);
//        if (StrUtil.isNotBlank(Entrepreneurs.getAreaCode())) {
//            List<String> list = JSON.parseObject(Entrepreneurs.getAreaCode(), new TypeReference<List<String>>() {
//            });
//            Entrepreneurs.setValue(list);
//        }
        return Result.success(Entrepreneurs);
    }

    //用户根据id查询
    @GetMapping("/getUserEntrepreneursById")
    public Result getUserEntrepreneursById(Integer id,Double longitude,Double latitude) {
        EntrepreneursModel Entrepreneurs = entrepreneursService.getUserById(id,longitude,latitude);
        return Result.success(Entrepreneurs);
    }

    //新增和修改
    @PostMapping("/addAndUpdateEntrepreneurs")
    public Result addAndUpdateEntrepreneurs(@RequestBody EntrepreneursModel entrepreneursModel) {
        EntrepreneursModel entrepreneurs = entrepreneursService.getById(entrepreneursModel.getId());
//        if (entrepreneursModel.getValue() != null) {
//            entrepreneursModel.setAreaCode(JSON.toJSONString(entrepreneursModel.getValue()));
//        }
        if (entrepreneurs != null) {
            entrepreneursModel.setUpdateTime(CommonHelp.getNowTimestamp());
            entrepreneursService.updateById(entrepreneursModel);
            return Result.success("修改成功");
        } else {
            entrepreneursModel.setCreateTime(CommonHelp.getNowTimestamp());
            entrepreneursService.save(entrepreneursModel);
            return Result.success("添加成功");
        }
    }
    //根据id删除
    @GetMapping("/delEntrepreneurs")
    public Result delEntrepreneurs(Integer id) {
        entrepreneursService.removeById(id);
        return Result.success("删除成功");
    }
}

